package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ACommentMapper;
import com.ruoyi.system.domain.AComment;
import com.ruoyi.system.service.IACommentService;

/**
 * 评论管理Service业务层处理
 *
 * @author system
 * @date 2025-02-23
 */
@Service
public class ACommentServiceImpl implements IACommentService {
    @Autowired
    private ACommentMapper aCommentMapper;

    /**
     * 查询评论管理
     *
     * @param id 评论管理主键
     * @return 评论管理
     */
    @Override
    public AComment selectACommentById(Long id) {
        return aCommentMapper.selectACommentById(id);
    }

    /**
     * 查询评论管理列表
     *
     * @param aComment 评论管理
     * @return 评论管理
     */
    @Override
    public List<AComment> selectACommentList(AComment aComment) {
        return aCommentMapper.selectACommentList(aComment);
    }

    /**
     * 新增评论管理
     *
     * @param aComment 评论管理
     * @return 结果
     */
    @Override
    public int insertAComment(AComment aComment) {
        aComment.setCreateTime(DateUtils.getNowDate());
        boolean flag = !aComment.getParentId().equals(0L);
        AComment info = aCommentMapper.selectACommentById(aComment.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if (flag && info == null && !UserConstants.DEPT_NORMAL.equals(info.getDelFlag()))
        {
            throw new ServiceException("分类停用，不允许新增");
        }
        String ancestors = "";
        if(null != info){
            ancestors = info.getAncestors() + ",";
        }
        aComment.setAncestors(ancestors + aComment.getParentId());
        return aCommentMapper.insertAComment(aComment);
    }

    /**
     * 修改评论管理
     *
     * @param aComment 评论管理
     * @return 结果
     */
    @Override
    public int updateAComment(AComment aComment) {
        aComment.setUpdateTime(DateUtils.getNowDate());
        AComment newParentCategory = aCommentMapper.selectACommentById(aComment.getParentId());
        AComment oldCategory = aCommentMapper.selectACommentById(aComment.getId());
        if (StringUtils.isNotNull(newParentCategory) && StringUtils.isNotNull(oldCategory))
        {
            String newAncestors = newParentCategory.getAncestors() + "," + newParentCategory.getId();
            String oldAncestors = oldCategory.getAncestors();
            aComment.setAncestors(newAncestors);
            updateACommentChildren(aComment.getId(), newAncestors, oldAncestors);
        }
        int result = aCommentMapper.updateAComment(aComment);
        if (UserConstants.DEPT_NORMAL.equals(aComment.getDelFlag()) && StringUtils.isNotEmpty(aComment.getAncestors())
                && !StringUtils.equals("0", aComment.getAncestors()))
        {
            // 如果该部门是启用状态，则启用该部门的所有上级部门
            updateParentCategoryStatusNormal(aComment);
        }
        return result;
    }

    /**
     * 批量删除评论管理
     *
     * @param ids 需要删除的评论管理主键
     * @return 结果
     */
    @Override
    public int deleteACommentByIds(Long[] ids) {
        return aCommentMapper.deleteACommentByIds(ids);
    }

    /**
     * 删除评论管理信息
     *
     * @param id 评论管理主键
     * @return 结果
     */
    @Override
    public int deleteACommentById(Long id) {
        return aCommentMapper.deleteACommentById(id);
    }

    /**
     * 批量逻辑删除评论管理
     *
     * @param ids 需要删除的评论管理主键
     * @return 结果
     */
    @Override
    public int logicDeleteACommentByIds(Long[] ids) {
        return aCommentMapper.logicDeleteACommentByIds(ids);
    }
    /**
     * 逻辑删除评论管理信息
     *
     * @param id 评论管理主键
     * @return 结果
     */
    @Override
    public int logicDeleteACommentById(Long id) {
        return aCommentMapper.logicDeleteACommentById(id);
    }

    /**
     * 校验名称是否唯一
     *
     * @param aComment 分类信息
     * @return 结果
     */
    @Override
    public boolean checkNameUnique(AComment aComment) {
        Long id = StringUtils.isNull(aComment.getId()) ? -1L : aComment.getId();
        AComment info = aCommentMapper.checkNameUnique(aComment.getComName(), aComment.getParentId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 根据ID查询所有子分类（正常状态）
     *
     * @param categoryId 分类ID
     * @return 子分类数
     */
    @Override
    public int selectNormalChildrenCategoryById(Long categoryId) {
        return aCommentMapper.selectNormalChildrenCategoryById(categoryId);
    }

    @Override
    public List<AComment> selectACommentXcxList(AComment aComment) {
        return aCommentMapper.selectACommentXcxList(aComment);
    }

    @Override
    public int updateACommentByLikeNum(Long id) {
        return aCommentMapper.updateACommentByLikeNum(id);
    }

    /**
     * 修改该分类的父级分类状态
     *
     * @param aComment 当前分类
     */
    private void updateParentCategoryStatusNormal(AComment aComment) {
        String ancestors = aComment.getAncestors();
        Long[] ids = Convert.toLongArray(ancestors);
        aCommentMapper.updateCategoryStatusNormal(ids);
    }

    /**
     * 修改子元素关系
     *
     * @param categoryId 被修改的分类ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateACommentChildren(Long categoryId, String newAncestors, String oldAncestors)
    {
        List<AComment> children = aCommentMapper.selectChildrenById(categoryId);
        for (AComment child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            aCommentMapper.updateCategoryChildren(children);
        }
    }
}
